package guiscreens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import dao.ResultSetTable;
/**
 * This class shows the result of the searching performed from the 
 * searchitem panel.
 * 
 * @author Lee
 * @version 1.0
 */
public class SearchResults extends JPanel {

	private CardLayout c1;
	private JPanel basePanel;
	private JFrame thisFrame;
	private ResultSet results;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JTable table;

	/**
	 * Create the panel.
	 */
	public SearchResults(final CardLayout c1, final JPanel basePanel, 
			final JFrame thisFrame, ResultSet results) {
		this.c1 = c1;
		thisFrame.setTitle("Search Results");
		thisFrame.setBounds(login.XCENTER - 450, login.YCENTER - 300, 900, 600);
		this.basePanel = basePanel;
		this.thisFrame = thisFrame;
		this.results = results;
		setBackground(Color.WHITE);
		setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("17px"),
				ColumnSpec.decode("69px"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(41dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(34dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(33dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("18px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(23dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(67dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		final ResultSetTable table = new ResultSetTable(results, true, false, false, false);
		table.setPreferredSize(new Dimension(825, 400));
		add(table, "2, 6, 11, 1, left, fill");
		table.addMouseListener(new MouseListener()
		{
			public void mouseClicked(MouseEvent e) {
				if (table.getSelectedColumn() == 0)
				{
					String value = (String) table.getValueAt(table.getSelectedRow(), 
							table.getSelectedColumn());
					String currentBid = (String) table.getValueAt(table.getSelectedRow(), 
							(table.getSelectedColumn() + 2));
					ItemFocus focusPanel = new ItemFocus(value, currentBid, c1, basePanel, 
							thisFrame);
					basePanel.add(focusPanel, "FOCUSCARD");
					c1.show(basePanel, "FOCUSCARD");
				}
			}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
		});
		
				JButton btnBackToSearch = new JButton("Back to Search");
				btnBackToSearch.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						thisFrame.setBounds(MainWindow.XCENTER - 200, MainWindow.YCENTER - 175, 400, 350);
						c1.show(basePanel, "SEARCHCARD");
					}
				});
				add(btnBackToSearch, "18, 10");
	}

}
